package autoteam.ui.dialogs;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;

public class PlayerCreator extends AutoTeamDialog
{
    private static final long serialVersionUID = 1599696151829784017L;
    
    private JButton addButton = new JButton("Add");
    private JButton closeButton = new JButton("Close");
    
    String[] DBteams = { "Bunny", "Fox", "Lynx", "Clevland Steamers" };

	private JTextField playerField;

	private JComboBox teams;

	private JTextField leagueField;

	private JTextField sportField;
    
    public PlayerCreator( JFrame dialogOwner )
    {
	super( dialogOwner );
	this.setTitle( "Add New Player" );
	this.setDialogSize( new Dimension(300,175) );
	
	JPanel buttonPane = new JPanel( );	
	buttonPane.add(addButton);
	buttonPane.add(closeButton);
	
	JPanel formPane = new JPanel( new GridLayout(4,2) );
	JLabel playerName = new JLabel( "Player Name:" );
	JLabel teamNames = new JLabel( "Teams:" );
	JLabel leagueName = new JLabel( "League Name:" );
	JLabel sportType = new JLabel( "Sport Type:" );
	
	playerField = new JTextField(25);
	teams = new JComboBox(  );
	leagueField = new JTextField(25);
	sportField = new JTextField(25);
	DataAccessObject dao = new DataAccessObject();
	List<DBRecord> teamRecs = dao.getAllRecords("teams");
	for (DBRecord rec : teamRecs)
	{
		teams.addItem(rec.getFieldValue("teamName"));
	}
	
	sportField.setEditable( false );
	leagueField.setEditable( false );
	
	formPane.add( playerName );
	formPane.add( playerField );
	
	formPane.add( teamNames );
	formPane.add( teams );

	formPane.add( sportType );
	formPane.add( sportField );
	
	formPane.add( leagueName );
	formPane.add( leagueField );

	Container contentPane = getContentPane( );
	setButtonActions( this );
	contentPane.add( formPane, BorderLayout.CENTER );
	contentPane.add( buttonPane, BorderLayout.SOUTH );
	
	pack( );
    }

    @ Override
    protected void setButtonActions( final AutoTeamDialog thisDialog )
    {
	addButton.addActionListener( new ActionListener( )
	{
	    @ Override
	    public void actionPerformed( ActionEvent e )
	    {
	    	DataAccessObject dao = new DataAccessObject();
	    	String[] fullname = playerField.getText().split(" ");
	    	
	    	//TODO: Modify teams table to accomodate new player
	    	dao.addRecord
	    	( 
	    	     "players", 
	    	     new ColumnData("firstname", fullname[0]),
	    	     new ColumnData("lastname", fullname[1]),
	    	     new ColumnData("biography", "(not available/created)"),
	    	     new ColumnData( "picturepath", "./trunk/img/profile_default.gif" )
	    	);
	    	
	    	thisDialog.close();
	    }
	} );
	
	closeButton.addActionListener( new ActionListener( )
	{
	    @ Override
	    public void actionPerformed( ActionEvent e )
	    {
		thisDialog.close();
	    }
	} );
	
    }   

}
